<template>
  <div v-show="this.$store.state.isShowAll" class="changeUser">
    <el-form>
      <!-- <el-form-item label="新建新用户"></el-form-item> -->
      <h2>更改用户</h2>
      <el-form-item label="ID">
        <el-input v-model="userId" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="用户名">
        <el-input v-model="userName" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="passWord" placeholder="" show-password></el-input>
      </el-form-item>
      <el-form-item label="权限">

        <el-select v-model="authority" disabled placeholder="请选择">
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          >
          </el-option>
        </el-select>

      </el-form-item>
      <el-form-item class="buttonBox">
        <el-button type="primary" @click="update">更改</el-button>
        <el-button type="warning" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<style scoped>
.changeUser {
  display: flex;
  justify-content: center;
  align-items: center;
  height: calc(100vh - 3.75rem);


}

.el-form {
  width: 21.875rem;

}

.el-select {
  display: flex;

}

element.style {
  margin-bottom: 0 !important;
}

.buttonBox {
  display: flex;
  justify-content: center;
}

</style>

<script>
export default {
  name: 'ChangeUser',

  data() {
    return {
      options: [
        {
          label: '管理员',
          value: 'admin'
        },
        {
          label: '注册用户',
          value: 'singedUser'
        }],
      authority: '',
      userName: '',
      passWord: '',
      userId: '',
      authority_o: '',
      userName_o: '',
      passWord_o: '',
      userId_o: ''
    }
  },
  mounted() {
    // console.log(this.$route)

    this.userName = this.$route.params.user.userName
    this.userId = this.$route.params.user.id
    this.passWord = this.$route.params.user.passWord

    this.userName_o = this.$route.params.user.userName
    this.userId_o = this.$route.params.user.id
    this.passWord_o = this.$route.params.user.passWord


  },
  methods: {
    cancel() {
      this.authority = ''
      this.userName = ''
      this.userId = ''
      this.passWord = ''
    },
    update() {

      let token = this.$store.state.userToken
      if (this.userName_o != this.userName) {
        let url = 'http://shilezhi.club:8082/Api/User/UpdateUser/name'
        let postData = {
          token: token,
          UserName: this.userName,
          id: this.userId
        }
        console.log(JSON.stringify(postData))
        this.axios.defaults.withCredentials = true;
        this.axios.defaults.headers.post["Content-Type"] =
            "application/json;charset=utf-8";
        this.axios.post(url, JSON.stringify(postData)).then(
            response => {
              if (response.data.code === 'ok') {
                this.$message.success('改名:成功')
              } else {
                this.$message.warning('改名:' + response.data.info)
              }
            },
            err => {
              console.log(err)
            }
        )
      }
      if (this.passWord_o != this.passWord) {
        let url = 'http://shilezhi.club:8082/Api/User/UpdateUser/passwd'
        let postData = {
          token: token,
          PassWord: this.passWord
        }
        this.axios.post(url, JSON.stringify(postData)).then(
            response => {
              if (response.data.code === 'ok') {
                this.$message.success('改密:成功')
              } else {
                this.$message.warning('改密:' + response.data.info)
              }
            },
            err => {
              console.log(err)
            }
        )
      }
    }
  },


};
</script>